/**
 * Created by znsw007 on 2015/12/17.
 */

(function (Framework7, T7, $$) {
    'use strict';
    function _currency(value) {
        var f1 = value;
        var f2 = (Math.round((f1 - 0) * 100)) / 100;
        f2 = Math.floor(f2) == f2 ? f2 + ".00" : (Math.floor(f2 * 10) == f2 * 10) ? f2 + '0' : f2;
        f2 = String(f2);
        var r = /(\d+)(\d{3})/;
        var fs = String(f2);
        while (r.test(f2)) {
            f2 = f2.replace(r, '$1' + ',' + '$2');
        }
        return f2;
    }
    T7.registerHelper('currency', function (value) {
        return _currency(value);
    });

    function _sex(value) {
        var cla = "";
        if (value == '男') {
            cla = "icon-boy";
        } else {
            cla = "icon-girl";
        }
        return cla;
    }
    T7.registerHelper('formatImg', function (value) {
        return 'http://39.106.126.22' + value
    })
    T7.registerHelper('sex', function (value) {
        return _sex(value);
    });
    T7.registerHelper('formatDate', function (stamp) {
        var newDate = moment(stamp).format("YYYY-MM-DD HH:mm:ss"); //do something here to modify 'date' string
        return newDate;
    });
    T7.registerHelper('orderStatus', function (state) {
        if (state == 0) {
            return "已提交";
        } else if (state == 1) {
            return "待处理";
        } else if (state == 2) {
            return "处理中";
        } else if (state == 3) {
            return "待评价";
        } else if (state == 4) {
            return "已完成";
        }
        return state;
    });
    T7.registerHelper('orderStatusColor', function (state) {
        if (state == 0) {
            return "color-black";
        } else if (state == 1) {
            return "orange";
        } else if (state == 2) {
            return "red";
        } else if (state == 3) {
            return "blue";
        }
        return state;
    });
    T7.registerHelper('formatRate', function (val) {
        return Number(val) * 100
    })
    T7.registerHelper('formatIndex', function (val) {
        return Number(val) + 1
    })
    T7.registerHelper('formatId', function (val) {
        return 'class-a-' + val
    })

    T7.registerHelper('photosCounts', function (state) {
        console.log(state)
        if (state == 0 || state == 1) {
            return (1);
        } else {
            return '(1/' + state + ')';
        }
        // else if(state == 1){
        //     return "orange";
        // }else if(state == 2){
        //     return "red";
        // }else if(state == 3){
        //     return "blue";
        // }
        // return state;
    });

    T7.registerHelper('getGoodsname', function (jsongoods) {
        console.log("列表物品" + JSON.stringify(jsongoods));
        if (utils.isEmpty(jsongoods) || jsongoods == "null" || jsongoods == "") {
            console.log("列表物品信息空" + jsongoods);
            return "";
        }
        var goodArr = eval(jsongoods);
        var goodsNames = [];
        for (var i = 0; i < goodArr.length; i++) {
            goodsNames.push(goodArr[i].goodsName);
        }
        return goodsNames.join();
    });

    T7.registerHelper('GoodsCheck', function (checkgoodids, thisgoodid) {
        console.log("列表物品" + JSON.stringify(checkgoodids));
        if (utils.isEmpty(checkgoodids) || checkgoodids == "null" || checkgoodids == "") {
            console.log("列表物品信息空" + checkgoodids);
            return "uncheck";
        }
        var goodidArr = eval(checkgoodids);
        /*for(var i=0;i<goodidArr.length;i++){
        }*/
        var position = goodidArr.indexOf(thisgoodid);
        if (position < 0) {
            return "uncheck"
        } else {
            return "check";
        }
    });

    T7.registerHelper('orderPic', function (photos) {
        // console.log(photos)
        if (utils.isEmpty(photos) || photos == "null" || photos == "") {
            console.log("列表图片信息空" + photos);
            return "img/120-120.png";
        }
        var picUrls = eval(photos);
        // console.log(picUrls)
        // console.log(photo_Path)
        if (picUrls.length > 0 && !utils.isEmpty(picUrls[0])) {
            // console.log(photo_Path + picUrls[0])
            return photo_Path + picUrls[0];
        }
        return "img/120-120.png";
    });
    T7.registerHelper('getGuzhang', function (state) {
        if (state == 0) {
            return "你的报修工单已提交，等待管理员分配，不要着急哦~";
        } else if (state == 1) {
            return "工单已分配，等师傅处理";
        } else if (state == 2) {
            return "师傅已经开始处理，请耐心等待~~~";
        } else if (state == 3) {
            return "师傅已经完成订单，请对本次维修服务做出评价";
        } else if (state == 4) {
            return "您的订单已经处理完成，感谢您的使用！"
        }
        return state;
    })
    T7.registerHelper('formatDesic', function (val) {
        if (val === null) return ''
        return '-' + val
    })
    function _role(value) {
        var cla = "";
        if (value == 1) {
            cla = "教师";
        } else {
            cla = "学生";
        }
        return cla;
    }
    T7.registerHelper('role', function (value) {
        return _role(value);
    });
    window._currency = _currency;

    /**
     * 使用f7需创建对象 设置成全局
     */
    var myApp = new Framework7({
        animateNavBackIcon: true,
        template7Pages: true,
        modalButtonOk: '确定',
        modalButtonCancel: '取消',
        cache: false,
        scroller: "auto",
        modalTitle: "",
        pushState: true
    });
    window.myApp = myApp;
    window.$$ = Dom7;
    window.T7 = Template7;
})(Framework7, Template7, Dom7);